package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugPresPermissionsConfBo;
import com.zp.drug.entity.vo.DrugPresPermissionsConfVo;
import com.zp.drug.service.IDrugPresPermissionsConfService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 处方权限配置控制器
 * 前端访问路由地址为:/drug/drugPresPermissionsConf
 *
 * @author zhang peng
 * @ date 2023-06-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugPresPermissionsConf")
public class DrugPresPermissionsConfController extends BaseController {

    private final IDrugPresPermissionsConfService iDrugPresPermissionsConfService;

    /**
     * 查询处方权限配置列表 (分页)
     */
    @SaCheckPermission("base:drugPresPermissionsConf:list")
    @GetMapping("/selectPage")
//    @Lock4j(keys = {"drugPresPerSelectPage"}, acquireTimeout = 1000, expire = 10000,
//            executor = CustomRedissonLockExecutor.class)
    public TableDataInfo<DrugPresPermissionsConfVo> selectPage (DrugPresPermissionsConfBo bo, PageQuery pageQuery) {
        return iDrugPresPermissionsConfService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询处方权限配置列表 (不分页)
     */
    @SaCheckPermission("base:drugPresPermissionsConf:list")
    @GetMapping("/list")
    public R<List<DrugPresPermissionsConfVo>> list (DrugPresPermissionsConfBo bo) {
        List<DrugPresPermissionsConfVo> list = iDrugPresPermissionsConfService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出处方权限配置列表
     */
    @SaCheckPermission("base:drugPresPermissionsConf:export")
    @Log(title = "处方权限配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (DrugPresPermissionsConfBo bo, HttpServletResponse response) {
        List<DrugPresPermissionsConfVo> list = iDrugPresPermissionsConfService.queryList(bo);
        ExcelUtil.exportExcel(list, "处方权限配置", DrugPresPermissionsConfVo.class, response);
    }

    /**
     * 获取处方权限配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:drugPresPermissionsConf:list")
    @GetMapping("/{id}")
    public R<DrugPresPermissionsConfVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugPresPermissionsConfService.queryById(id));
    }

    /**
     * 新增处方权限配置
     */
    @SaCheckPermission("base:drugPresPermissionsConf:add")
    @Log(title = "处方权限配置", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody DrugPresPermissionsConfBo bo) {
        return toAjax(iDrugPresPermissionsConfService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改处方权限配置
     */
    @SaCheckPermission("base:drugPresPermissionsConf:edit")
    @Log(title = "处方权限配置", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody DrugPresPermissionsConfBo bo) {
        return toAjax(iDrugPresPermissionsConfService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除处方权限配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:drugPresPermissionsConf:remove")
    @Log(title = "处方权限配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDrugPresPermissionsConfService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 同步机构人员
     *
     * @param orgId 机构id
     */
    @Log(title = "处方权限配置", businessType = BusinessType.UPDATE)
    @PutMapping("/syncOrgPerson/{orgId}")
    public R<Void> remove (@NotEmpty(message = "机构不能为空") @PathVariable String orgId) {
        return toAjax(iDrugPresPermissionsConfService.syncOrgPerson(orgId) ? 1 : 0);
    }
}
